<?php defined('SYS_PATH') or die('No direct script access.');
/**
 * View class
 *
 * @author zendzhang<zendzhang@hotmail.com>
 * @version $Id: View.php 9 2013-01-14 03:24:50Z zendzhang@hotmail.com $
 */
class TrueBlood_View
{
    /**
     * variables that are later available to the view
     *
     * @var array
     */
    protected $_vars = array();

    /**
     * Assigns values to template variables
     *
     * @param mixed $name
     * @param mixed $value
     * @return void
     */
    public function assign($name, $value = null)
    {
        if (is_array($name))
        {
            $this->_vars = array_merge($this->_vars, $name);
        } elseif (is_object($name))
        {
            foreach ($name as $key => $val)
            {
                $this->_vars[$key] = $val;
            }
        }
        else
        {
            $this->_vars[$name] = $value;
        }
    }

    /**
     * Fetchs the view file and extracts the variables.
     *
     * @param string $tpl
     * @param array $vars
     * @return string
     */
    public function fetch($tpl, $vars = array())
    {
        $view = realpath(APP_PATH . '/views/' . $tpl . '.php');
        if (!$view || !is_file($view))
        {
            throw new TrueBlood_Exception("Unable to load views:{$tpl}.php");
        }
        if (is_array($vars) && !empty($vars))
        {
            $this->_vars = array_merge($this->_vars, $vars);
        }
        extract($this->_vars);
        ob_start();
        include $view;
        $buffer = ob_get_contents();
        @ob_end_clean();
        return $buffer;
    }

    /**
     * Renders or returns the output of view
     *
     * @param string $tpl
     * @param array $vars
     * @param bool $return
     * @return string
     */
    public function render($tpl, $vars = array(), $return = false)
    {
        if ($return)
        {
            return $this->fetch($tpl, $vars);
        }
        else
        {
            echo $this->fetch($tpl, $vars);
        }
    }

    /**
     * Display view
     *
     * @param string $tpl
     * @param array $vars
     * @return void
     */
    public function display($tpl, $vars = array())
    {
        echo $this->fetch($tpl, $vars);
    }

    /**
     * Magic method, Retrieve an assigned variable
     *
     * @param string $name
     * @return mixed
     */
    public function __get($name)
    {
        if (isset($this->_vars[$name]))
        {
            return $this->_vars[$name];
        }
        return null;
    }

    /**
     * Magic method, Assign a variable to the template
     *
     * @param string $name
     * @return void
     */
    public function __set($name, $value)
    {
        $this->assign($name, $value);
    }

    /**
     * Magic method, Allows testing with empty() and isset() to work
     *
     * @param string $name
     * @param bool $
     */
    public function __isset($name)
    {
        return isset($this->_vars[$name]);
    }

    /**
     * Magic method, Allows unset() on object properties to work
     *
     * @param string $name
     * @return void
     */
    public function __unset($name)
    {
        if (isset($this->_vars[$name]))
        {
            unset($this->_vars[$name]);
        }
    }
}

?>